package gamagora.game.entity
{
	import gamagora.game.resources.OSImages;
	
	import org.flixel.FlxG;

	public class OSCage extends OSEntity
	{
		private static const TIME_BEFORE_MOVE:Number = 10;
		private static const BACKGROUND_CAGE_WIDTH:Number = 128;
		private static const BACKGROUND_CAGE_HEIGHT:Number = 32;
		private static const NORMAL_CAGE_WIDTH:Number = 256;
		private static const NORMAL_CAGE_HEIGHT:Number = 64;
		private static const DIST_BEFORE_FOLLOW:Number = 300;
		
		private var _cage:OSEntity;
		private var _background:Boolean;
		private var _moveTimer:Number;
		private var _halfWidth:Number;
		private var _playerDist:Number;
		
		public function OSCage(X:Number, Y:Number, background:Boolean = true)
		{
			super(ANIMATION, X, Y);
			_hide = true;
			_background = background;
			_moveTimer = 0;
			_playerDist = 0;
			
			if(_background)
			{
				_cage = new OSEntity(NOTHING, X, Y, OSImages.CAGE);
				add(new OSMonkey(x + (BACKGROUND_CAGE_WIDTH * 0.25), y, true));
				add(new OSMonkey(x + (BACKGROUND_CAGE_WIDTH * 0.75), y, true));
				_halfWidth = BACKGROUND_CAGE_WIDTH * 0.5;
			}
			else 
			{
				_cage = new OSGround(X, Y, NORMAL_CAGE_WIDTH, NORMAL_CAGE_HEIGHT, 0, false, false);
				_cage.loadGraphic(OSImages.CAGE2);
				add(new OSMonkey(x + (NORMAL_CAGE_WIDTH * 0.25), y - 16));
				add(new OSMonkey(x + (NORMAL_CAGE_WIDTH * 0.75), y - 16));
				_halfWidth = NORMAL_CAGE_WIDTH * 0.5;
			}
			
			add(_cage);
		}
		
		public override function update():void
		{
			super.update();
			
			if(_background)
			{
				updateRandomMoves();
			}
			else
			{
				_playerDist = _world.getPlayerX() - (x + _halfWidth); 
				if(Math.abs(_playerDist) < DIST_BEFORE_FOLLOW) //le joueur est proche de la cage, les singes vont le suivre
				{
					updateFollowPlayer();
				}
				else 
				{
					updateRandomMoves();
				}
			}
			
		}
		
		private function updateRandomMoves():void
		{
			_moveTimer += FlxG.elapsed;
			if(_moveTimer >= TIME_BEFORE_MOVE)
			{
				_moveTimer = 0;
				
				var monkey:OSMonkey = _entities[uint(Math.random() * 2)] as OSMonkey;
				if(!monkey.move) //Hey ho je bouge déjà, ca va hein !
				{
					if(uint(Math.random() * 2) == 0) // On bouge ?
					{
						if(monkey.x > x + _halfWidth) // Il y a plus de place a gauche
						{
							monkey.moveFrom(-(_halfWidth * Math.random() * 0.5 + 10), false);
						}
						else //Finalement à droite c'est mieux
						{
							monkey.moveFrom(_halfWidth * Math.random() * 0.5 + 10, false);
						}
					}
				}
			}
		}
		
		private function updateFollowPlayer():void
		{
			if(_playerDist > 0) // Go right
			{
				if(_entities[0].x < (x + NORMAL_CAGE_WIDTH - 70) && !(_entities[0] as OSMonkey).move)
				{
					(_entities[0] as OSMonkey).moveFrom((x + NORMAL_CAGE_WIDTH - 70) - _entities[0].x, false);
				}
				
				if(_entities[1].x < (x + NORMAL_CAGE_WIDTH - 40) && !(_entities[1] as OSMonkey).move)
				{
					(_entities[1] as OSMonkey).moveFrom((x + NORMAL_CAGE_WIDTH - 40) - _entities[1].x, false);
				}
			}
			else  // Go left
			{
				if(_entities[0].x > (x + 40) && !(_entities[0] as OSMonkey).move)
				{
					(_entities[0] as OSMonkey).moveFrom((x + 40) - _entities[0].x, false);
				}
				
				if(_entities[1].x > (x + 70) && !(_entities[1] as OSMonkey).move)
				{
					(_entities[1] as OSMonkey).moveFrom((x + 70) - _entities[1].x, false);
				}
			}
		}
	}
}
